from decimal import Decimal
from oscar.apps.catalogue.abstract_models import AbstractProduct

class Product(AbstractProduct):
    pass

    def min_variant_vendor_price(self, user):
        if self.is_group:
            if self.min_variant_price_incl_tax:
                price =  Decimal(self.min_variant_price_incl_tax) \
                    * Decimal(user.get_profile().commission)
            return price
        return None

    def vendor_price(self, user):
        if not self.is_group:
            if self.has_stockrecord:
                if self.stockrecord.price_incl_tax:
                    price =  Decimal(self.stockrecord.price_incl_tax) \
                        * Decimal(user.get_profile().commission)
                return price
        return None

    def size(self):
        if self.is_group:
            return ''

        for value in self.attribute_values.select_related().all():
            if value.attribute.name == 'talle':
                return value.value
        return ''

    def peso(self):
        if self.is_group:
            return 0.0

        values = self.attribute_values
        for value in values.select_related().all():
            if value.attribute.name == 'peso':
                try:
                    return float(value.value)
                except exceptions.ValueError:
                    pass
        return 0.0

    def color(self):
        values = self.attribute_values
        if not self.is_group:
            values = self.parent.attribute_values

        for value in values.select_related().all():
            if value.attribute.name == 'color':
                return value.value
        return ''

    def attribute_summary(self):
        """
        Return a string of all of a product's attributes
        """
        pairs = []
        for value in self.attribute_values.select_related().all():
            pairs.append("%s: %s" % (value.attribute.name,
                                     value.value))
        return ", ".join(pairs)

    def variants_ordered_by_size(self):
        return sorted(self.variants.all(), key=lambda variant: variant.size())

from oscar.apps.catalogue.receivers import *
